<?
import ("stock_price_LivePriceView");
import ("stock_analyser_AnalyserConfig");
class stock_analyser_TableView extends jsml_View{
	public static function main(){
		_($__document)->ready(function(){
			$config = stock_analyser_AnalyserConfig::$config;
			$view = $config->getBean($config->TableView);
			_($__document->body)->append($view->div);
			$view->view();
		});
	}
	
	public $capital = 200000;
	public $capPer = 0.34;
	public $arrayRate = array(
		 "VRC" => -989000000
		,"PNJ" => -989000000
		
		,"CMT" => -9890000
		,"SVN" => -9890000
		,"KSS" => -9890000
		,"AAA" => -9890000
		,"TCM" => -9890000
		,"SJE" => -9890000
		,"DCS" => -9890000
		,"PXT" => -9890000
		,"BBC" => -9890000
		,"PGS" => -9890000
		,"SFI" => -9890000
		,"SSI" => -9890000
		,"BMC" => -9890000
		,"VID" => -9890000
	);
	
	public function construct($key) {
		parent::construct($key);
		if ($this->config->TableView == $key) {
		}
		return $this;
	}

	//public $filterType = "valueSub";
	public $filterType = "minClose";
	//public $filterType = "value2";
	public function view(){
		_($this)->bind("rpc.queryMap", function($event, $result){ $this->onRpc("queryMap", $event, $result); });
		$this->queryMap($this->filterType);
		setInterval(function(){
			$this->queryMap($this->filterType);
		},30000);
		$_($html)->appendTo($this->div);
		return $this;
	}
	
	public $tableDtMap;
	public function onRpc($key, $event, $result){
		if("queryMap" == $key){
			$dtMap = $result["return"];
			$this->tableDtMap = $dtMap;
			$html = $this->htmlTable($dtMap);
			$this->div->html($html);
		}
	}

	public function htmlTable($dtMap){
?>
<$html>
<table border=1 cellpadding="0" cellspacing="0" style="border-color:#333333;border-collapse:collapse;width:100%">
<? 
$count = 0;
foreach($dtMap as $symbol => $dt){
	$count++;
	if($count==1){
?>
<tr style="background-color:#DBDBDB;height:20">
<?
	foreach($dt as $key => $value){
		if(strpos($key, "STT__")===0 || $key == "minMax3" || $key == "minClose3" || $key == "maxClose3" || $key == "value" || $key == "value2Percent") continue;
		if($key == "rate") $key = $key."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
?>
	<td><?= $key ?></td>
<?
	}
?>
</tr>
<?
	}
?> <?= $this->htmlTrTable($dt, $count) ?>
<? } ?>
</table>
</$html>
<?
		return $html;
	}
	
	public function getPercentColor($percent){
		if($percent > 4.0) return "#E100E1";
		if($percent < -4.0) return "#66CCFF";
		//if($percent == 0) return "#F7FF31";
		if($percent > 0) return "#00D700";
		if($percent <= 0) return "#EC0000";
		return "#EC0000";
	}
	
	public function formatValue($value){
		if($value==null) return null;
		return number_format($value/1000000,1,".",",");
	}
	
	public function getStockColor($dt){
		if($dt==null) return '#00D700'; 
		if($dt["rate"] < -5000) return "#00D700";
		//if($dt["rate"] < -3000) return "#FF00FF";
		if($dt["rate"] < -3000) return "white";
		return '#00D700';
	}
	public function getStockFontWeight($dt){
		if($dt==null) return 'normal'; 
		if($dt["rate"] < -5000) return "normal";
		//if($dt["rate"] < -3000) return "#FF00FF";
		if($dt["rate"] < -3000) return "bold";
		return 'normal';
	}
	public function getStockStyle($dt){
		if($dt==null) return 'text-align:center;'; 
		if($dt["rate"] < -900000) return "text-align:center;";
		if($dt["rate"] < -9000) return "text-align:right;";
		if($dt["rate"] < -3000) return "text-align:left;";
		//if($dt["rate"] < -3000) return "#FF00FF";
		//if($dt["rate"] < -3000) return "text-align:right;";
		return 'text-align:left;';
	}
	
	public function htmlTotalTable(){
		$tableDt = $this->tableDtMap["TOTAL"];
		$bgColor1 = "#444444";
		$livePriceView = new stock_price_LivePriceView();
?>
<$html>
<table align="right">
<tr>
<td>
	<?= $this->filterType ?>
</td>
<td style="background-color:<?= $bgColor1 ?>; font-weight:bold">
<? foreach($tableDt as $key=>$dt){ 
	if(strpos($key, "closeP")===0 && $key!="closePercent"){
		$color = $this->getPercentColor($dt - 50);
?>
<span style="color:<?= $color ?>"><?= $livePriceView->formatPrice($dt) ?></span>&nbsp|&nbsp
<?	} 
}
?>
</td>
</tr>
</table>
</$html>
<?
		return $html;
	}
	
	public function htmlTrTable($dt, $count){
		$livePriceView = new stock_price_LivePriceView();
		$bgColor1 = "";
		if($count%2==0) $bgColor1 = "#444444";
		else $bgColor1 = "#111111";
		$bgColor2 = "";
		if($count%2==0) $bgColor2 = "#223344";
		else $bgColor2 = "#112233";
		$bgColor1 = "#222222";
		$bgColor1 = "black";
		$bgColor2 = "#222222";
		//$bgColor2 = $bgColor1;
		//$bgColor2 = "#334455";
?>
<$html>
<tr style="text-align:right;">
<?
	foreach($dt as $key => $value){
		if(strpos($key, "STT__")===0 || $key == "minMax3" || $key == "minClose3" || $key == "maxClose3" || $key == "value" || $key == "value2Percent") continue;
		$color = "#00FF00";
		$align = "left";
		$styleTotal = "";
		if($dt["symbol"]=="TOTAL") $styleTotal = "font-weight:bold";
		if($key == "symbol" || $key == "buyVolx1" || $key == "buyVolx2" || $key == "buyVolx3" || $key == "value2" || $key == "closePercent" || $key == "market" || $key == "rate" || $key == "megaCount" || $key == "minMax" || $key == "minClose" ) {
			$color = $this->getPercentColor($dt["closePercent"]);
		} else if(strpos($key, "closeP")===0){
			if($dt["symbol"]=="TOTAL"){
				$color = $this->getPercentColor($dt[$key] - 50);
			}
			else $color = $this->getPercentColor($dt[$key]);
		}
		if(strpos($key, "closeP")===0 || $key == "rate" || $key == "buyVolx1" || $key == "buyVolx2" || $key == "buyVolx3" || $key == "closePercent"){
			$align = "right";
		} else {
			$align = "center";
		}
		if($key == "symbol"){
			//$stockColor = $this->getStockColor($dt);
			$value = '<a tag="aSymbol" href="" onclick="return false;" style="color:'.$color.'">'.$value."</a>";
		}
		if($key == "symbol" || $key == "buyVolx1" || $key == "buyVolx2" || $key == "buyVolx3" || $key == "value2" || $key == "closePercent" || $key == "market"){
			$bgColor = $bgColor2;
		} else {
			$bgColor = $bgColor1;
		}
		if($key == "value" || $key == "value2") $value = $this->formatValue($value);
		else if($key == "ceilPercent" || $key == "closePercent" || $key == "minMax" || $key == "minClose" || $key == "maxClose" || strpos($key, "closeP")===0){
			$value = $livePriceView->formatPrice($value);
			if($value==null) $value="0.0";
		}
		
		$keySTT = "STT__".$key;
		$valueSTT = $dt[$keySTT];
		if($valueSTT!=null){
			$value = "[".$valueSTT."]".$value."";
		}
?>
	<td style="<?= $styleTotal ?>;background-color:<?= $bgColor ?>;color:<?= $color ?>;text-align:<?= $align ?>"><?= $value ?></td>
<?
	}
?>
</tr>
</$html>
<?
		return $html;
	}
	
	public $priceDtArrayMapFile = "WEB-INF/files/stock/analyser/priceDtArrayMap.json";
	public function deletePriceDtArrayMap(){
		if(file_exists($this->priceDtArrayMapFile)) unlink($this->priceDtArrayMapFile);
	}
	public $tableDtArrayMapFile = "WEB-INF/files/stock/analyser/tableDtArrayMap.json";
	public function queryMap($filterType){
		//<@ $this->jsonp($filterType);
		function sortFunc1($a, $b){				
			if($a[0][stock_analyser_TableView::$sortColumn] > $b[0][stock_analyser_TableView::$sortColumn]) return -1;
			if($a[0][stock_analyser_TableView::$sortColumn] < $b[0][stock_analyser_TableView::$sortColumn]) return 1;
			return 0;
		}
		function sortFunc($a, $b){
			//if($a["symbol"] == "TOTAL" || $a[stock_analyser_TableView::$sortColumn] == null) return -1;
			//if($b["symbol"] == "TOTAL" || $b[stock_analyser_TableView::$sortColumn] == null) return 1;
			if($a[stock_analyser_TableView::$sortColumn] > $b[stock_analyser_TableView::$sortColumn]) return -1;
			if($a[stock_analyser_TableView::$sortColumn] < $b[stock_analyser_TableView::$sortColumn]) return 1;
			return 0;
		}
		
		function sortFuncInverse($a, $b){
			//if($a["symbol"] == "TOTAL" || $a[stock_analyser_TableView::$sortColumn] == null) return 1;
			//if($b["symbol"] == "TOTAL" || $b[stock_analyser_TableView::$sortColumn] == null) return -1;
			if($a[stock_analyser_TableView::$sortColumn] < $b[stock_analyser_TableView::$sortColumn]) return -1;
			if($a[stock_analyser_TableView::$sortColumn] > $b[stock_analyser_TableView::$sortColumn]) return 1;
			return 0;
		}
		$priceDtArrayMap = $this->loadJson($this->priceDtArrayMapFile);
		if($priceDtArrayMap==null){
			$sql = "SELECT * 
				FROM stock_price_PriceVo 
				WHERE stockDate>=DATE_SUB(NOW(), INTERVAL 200 DAY)
				ORDER BY stockDate DESC";
			$stm = $this->ds->prepare($sql);
			$stm->execute();
			$priceDtArray = $stm->fetchAll(PDO::FETCH_ASSOC);
			foreach($priceDtArray as $i=>$priceDt){
				$symbol = $priceDt["stock_symbol"];
				if($priceDtArrayMap[$symbol]==null) $priceDtArrayMap[$symbol]=array();
				array_push($priceDtArrayMap[$symbol],$priceDt);
			}
			$this->saveJson($this->priceDtArrayMapFile, $priceDtArrayMap);
		}
		//print_p($priceDtArrayMap["VCG"]);
		
		$livePriceView = new stock_price_LivePriceView();
		$livePriceDtMap = $livePriceView->queryMapAll();
		foreach($livePriceDtMap as $symbol=>$livePriceDt){
			$symbol = $livePriceDt["stock_symbol"];
			$livePriceDt["__livePrice__"]=1;
			if($priceDtArrayMap[$symbol]==null) $priceDtArrayMap[$symbol]=array();
			//if($priceDtArrayMap[$symbol][0]["stockDate"]==$livePriceDt["stockDate"]){
			if($priceDtArrayMap[$symbol][0]["__livePrice__"]==1){
				$this->priceDtArrayMap[$symbol][0] = $livePriceDt;
			} else {
				array_unshift($priceDtArrayMap[$symbol], $livePriceDt);
			}
		}
		//print_p($priceDtArrayMap["VCG"]);
		
		foreach($priceDtArrayMap as $symbol => &$priceDtArray){
			$chartView = new stock_analyser_ChartView();
			$chartView->priceDtArray = &$priceDtArray;
			$chartView->makeFirst();
		}
		unset($priceDtArray);
		//print_p($priceDtArrayMap["VCG"]);
				
		if($filterType==null) $filterType = "minClose";

		$livePriceVo = new stock_price_LivePriceVo();
		$livePriceDtArray = array();
		/*
		$sql = "SELECT * FROM `stock_price_livepricevo` WHERE 
			TIME(stockDate) = '10:30:00' AND DATE(stockDate) >= '".stock_analyser_ChartView::$startDate."'
			ORDER BY stockDate DESC";
		$stm = $this->ds->prepare($sql);
		$stm->execute();
		$livePriceDtArray = $stm->fetchAll(PDO::FETCH_ASSOC);
		foreach($livePriceDtArray as $i=>$livePriceDt){
			$symbol = $livePriceDt["stock_symbol"];
			$stockDate = $livePriceDt["stockDate"];
			//$livePriceDtArray[$i]["valueSub"] = $livePriceVo->getLiveVolume($livePriceDt);//;$livePriceVo->getLiveValueSub($livePriceDt);
		}

		stock_analyser_TableView::$sortColumn = "valueSub";
		uasort($livePriceDtArray, "sortFunc");
		
		//print_p($livePriceDtArray);
		
		$liveMap = array();
		foreach($livePriceDtArray as $i=>$livePriceDt){
			$symbol = $livePriceDt["stock_symbol"];
			//if($liveMap[$symbol]==null) $liveMap[$symbol] = $livePriceDt;
			if($liveMap[$symbol]==null) {
				$liveMap[$symbol] = array();
				$liveMap[$symbol]["valueSubMax"] = $livePriceDt["valueSub"];
				$liveMap[$symbol]["valueSub"] = 0;
				$liveMap[$symbol]["count"] = 0;
			}
			$liveMap[$symbol]["valueSub"] += $livePriceDt["valueSub"];
			$liveMap[$symbol]["count"] ++;
		}
		*/
		
		unset($priceDtArrayMap["VNINDEX"]);
		unset($priceDtArrayMap["HAINDEX"]);
		unset($priceDtArrayMap["VNindex"]);
		unset($priceDtArrayMap["vnindex"]);
		unset($priceDtArrayMap["NKD"]);
		unset($priceDtArrayMap["TMS"]);
		
		//////////////////////////////////
		unset($priceDtArrayMap["BCC"]);
		
		
		foreach($priceDtArrayMap as $symbol => &$priceDtArray){
			$chartView = new stock_analyser_ChartView();
			$chartView->priceDtArray = &$priceDtArray;
			$chartView->makeMa(1);
			$chartView->makePrice(20);
		}
		unset($priceDtArray);
		
		$i=0;
		foreach($priceDtArrayMap as $symbol=>&$priceDtArray){
			if($this->arrayRate[$symbol]!=null) continue;
			$i++;
			if($filterType=="minClose") {
				//if($i>=50) unset($priceDtArrayMap[$symbol]);
				//if($priceDtArray[0]["value2"]<2000000 && $priceDtArray[0]["value"]<2000000) unset($priceDtArrayMap[$symbol]);
				//if($priceDtArray[0]["value2"]<5000000) unset($priceDtArrayMap[$symbol]);
				//if($priceDtArray[0]["isDevided"]==1) unset($priceDtArrayMap[$symbol]);
				//if($priceDtArray[0]["megaCount"]<=2.0) unset($priceDtArrayMap[$symbol]);
				//if($priceDtArray[0]["market"]=="ho") unset($priceDtArrayMap[$symbol]);
				if($priceDtArray[0]["value2"]<300000 && $priceDtArray[0]["value"]<1000000) unset($priceDtArrayMap[$symbol]);
				if($priceDtArray[0]["minMaxZ"]<=50) unset($priceDtArrayMap[$symbol]);
			}
		}
		
		stock_analyser_TableView::$sortColumn = "minMaxZ";
		uasort($priceDtArrayMap, "sortFunc1");
		/*
		$i=0;
		foreach($priceDtArrayMap as $symbol=>&$priceDtArray){
			if($this->arrayRate[$symbol]!=null) continue;
			$i++;
			if($filterType=="minClose") {
				if($i>=150) unset($priceDtArrayMap[$symbol]);
			}
		}
		unset($priceDtArray);
		*/
		$i=0;
		foreach($priceDtArrayMap as $symbol=>&$priceDtArray){
			//$priceDtArrayMap[$symbol][0]["valueSub"] = $livePriceVo->getLiveValueSub($priceDtArrayMap[$symbol][0]);
			if($liveMap[$symbol]["count"] != 0) $priceDtArrayMap[$symbol][0]["valueSub"] = $liveMap[$symbol]["valueSub"] / $liveMap[$symbol]["count"];
			$priceDtArrayMap[$symbol][0]["valueSubMax"] = $liveMap[$symbol]["valueSubMax"];
			$priceDtArrayMap[$symbol][0]["value"] = $livePriceVo->getLiveValue($priceDtArrayMap[$symbol][0]);
			if($priceDtArrayMap[$symbol][0]["volume1"]!=0) {
				$priceDtArrayMap[$symbol][0]["valuePercentMax"] = $priceDtArrayMap[$symbol][0]["valueSubMax"]/$priceDtArrayMap[$symbol][0]["volume1"];
				//$priceDtArrayMap[$symbol][0]["valuePercent"] = $priceDtArrayMap[$symbol][0]["valueSub"]/$priceDtArrayMap[$symbol][0]["volume1"];
				$priceDtArrayMap[$symbol][0]["valuePercent"] = $livePriceVo->getLiveVolume($priceDtArrayMap[$symbol][0])/$priceDtArrayMap[$symbol][0]["volume1"];
			}
			
			//if($priceDtArrayMap[$symbol][0]["ma2"]!=0) $priceDtArrayMap[$symbol][0]["maPercent"] = 100*($priceDtArrayMap[$symbol][0]["high"] - $priceDtArrayMap[$symbol][0]["ma2"]) / $priceDtArrayMap[$symbol][0]["ma2"];
			
			//if($priceDtArrayMap[$symbol][0]["value"] > $priceDtArrayMap[$symbol][0]["value2"]) $priceDtArrayMap[$symbol][0]["value2"] = $priceDtArrayMap[$symbol][0]["value"];
			if($this->arrayRate[$symbol]!=null) continue;
			$i++;
			if($filterType=="minClose") {
				//if($i>1) unset($priceDtArrayMap[$symbol]);
				//if($priceDtArray[0]["value2"]<300000 && $priceDtArray[0]["value"]<1000000) unset($priceDtArrayMap[$symbol]);
				//if($priceDtArray[0]["value2"]<2000000) unset($priceDtArrayMap[$symbol]);
				//if($priceDtArray[0]["isDevided"]==1) unset($priceDtArrayMap[$symbol]);
				//if($priceDtArray[0]["megaCount"]<2.5) unset($priceDtArrayMap[$symbol]);
			}
			if($filterType=="value2") {
				//if($priceDtArray[0]["value2"]<1000000 && $priceDtArray[0]["value"]<1000000) unset($priceDtArrayMap[$symbol]);
				//if($i>=100) unset($priceDtArrayMap[$symbol]);
				//if($priceDtArray[0]["value2"]<1000000 && $priceDtArray[0]["value"]<1000000) unset($priceDtArrayMap[$symbol]);
			}
		}
		unset($priceDtArray);
		
		/*
		uasort($priceDtArrayMap,"sortFunc1");
		$rating = array(
			array("stock_symbol" => "DCS", "rating" => 1000),
			array("stock_symbol" => "IJC", "rating" => 1000)
		);
		$i=0;
		foreach($priceDtArrayMap as $symbol=>&$priceDtArray){
			$i++;
			if($i>=100) unset($priceDtArrayMap[$symbol]);
		}
		
		if($mapCond != null){
			$maCode = $mapCond["maCode"];
			$maCode1 = $mapCond["maCode1"];
			$maCode2 = $mapCond["maCode2"];
			if($maCode != null && $maCode1 != null && $maCode2 != null){
				foreach($priceDtArrayMap as $symbol=>&$priceDtArray){
					$chartView = new stock_analyser_ChartView();
					$chartView->priceDtArray = &$priceDtArray;
					$chartView->makeMa(3);
					//print_p($priceDtArray[1]);
					if($priceDtArray[0]["maCode"] != $maCode) unset($priceDtArrayMap[$symbol]);
					if($priceDtArray[1]["maCode"] != $maCode1) unset($priceDtArrayMap[$symbol]);
					if($priceDtArray[2]["maCode"] != $maCode2) unset($priceDtArrayMap[$symbol]);
				}
			}
			//print_p($maCode);
		}
		*/
		
		//$livePriceView = new stock_price_LivePriceView();
		//$livePriceMap = $livePriceView->queryMapAll();
		$tableMap = array();
		$tableMap["TOTAL"] = array();
		$tableMap["TOTAL"]["rate"] = "";
		$tableMap["TOTAL"]["ceilPercent"] = "";
		$tableMap["TOTAL"]["maPercent1"] = "";
		$tableMap["TOTAL"]["maPercent2"] = "";
		$tableMap["TOTAL"]["maPercent12"] = "";
		
		$tableMap["TOTAL"]["maPer1"] = "";
		$tableMap["TOTAL"]["maPer12"] = "";
			
		$tableMap["TOTAL"]["value"] = "";
		//$tableMap["TOTAL"]["valueSub"] = "";
		//$tableMap["TOTAL"]["valueSubMax"] = "";
		$tableMap["TOTAL"]["megaCount"] = "";
		$tableMap["TOTAL"]["minMax"] = "";
		$tableMap["TOTAL"]["minClose"] = "";
		$tableMap["TOTAL"]["maxClose"] = "";
		//$tableMap["TOTAL"]["minMaxZ"] = "";
		//$tableMap["TOTAL"]["minCloseZ"] = "";
		//$tableMap["TOTAL"]["maxCloseZ"] = "";
		
		$tableMap["TOTAL"]["value2"] = "";
		$tableMap["TOTAL"]["value2Percent"] = "";
		$tableMap["TOTAL"]["valuePercent"] = "";
		$tableMap["TOTAL"]["valuePercentMax"] = "";
		$tableMap["TOTAL"]["closePercent"] = "";
		$tableMap["TOTAL"]["market"] = "";
		$tableMap["TOTAL"]["buyVolx3"] = "";
		$tableMap["TOTAL"]["buyVolx2"] = "";
		$tableMap["TOTAL"]["buyVolx1"] = "";
		$tableMap["TOTAL"]["symbol"] = "TOTAL";
		
		$total = array();
		foreach($priceDtArrayMap as $stock_symbol => $dtArray){
			$dt = $dtArray[0];
			$tableMap[$stock_symbol] = array();
			$tableMap[$stock_symbol]["rate"] = 0;
			if($dt["ref"]!=0) $tableMap[$stock_symbol]["ceilPercent"] = round( 100*($dt["close"] - $dt["ceil"])/$dt["ref"], 2);
			//if($dt["sellVolume1"]==0 || $dt["sellVolume1"]==null) $tableMap[$stock_symbol]["ceilPercent"] = -4;
			
			$tableMap[$stock_symbol]["maPercent1"] = round($dt["maPercent1"], 2);
			$tableMap[$stock_symbol]["maPercent2"] = round($dt["maPercent2"], 2);
			$tableMap[$stock_symbol]["maPercent12"] = round($dt["maPercent12"], 2);
			
			$tableMap[$stock_symbol]["maPer1"] = round($dt["maPer1"], 2);
			$tableMap[$stock_symbol]["maPer12"] = round($dt["maPer12"], 2);
			
			$tableMap[$stock_symbol]["value"] = $dt["value"];
			//$tableMap[$stock_symbol]["valueSub"] = $dt["valueSub"];
			//$tableMap[$stock_symbol]["valueSubMax"] = $dt["valueSubMax"];
			//$tableMap[$stock_symbol]["buyVolumeF"] = $dt["buyVolumeF"];
			//if($tableMap[$stock_symbol]["buyVolumeF"]==null) $tableMap[$stock_symbol]["buyVolumeF"]=0;
			
			$tableMap[$stock_symbol]["megaCount"] = round($dt["megaCount"], 2);
			$tableMap[$stock_symbol]["minMax"] = $dt["minMax"];
			$tableMap[$stock_symbol]["minClose"] = $dt["minClose"];
			$tableMap[$stock_symbol]["maxClose"] = $dt["maxClose"];
			//$tableMap[$stock_symbol]["minMaxZ"] = $dt["minMaxZ"];
			//$tableMap[$stock_symbol]["minCloseZ"] = $dt["minCloseZ"];
			//$tableMap[$stock_symbol]["maxCloseZ"] = $dt["maxCloseZ"];
			
			$tableMap[$stock_symbol]["value2"] = $dt["value2"];
			if($dt["value2"]!=0) $tableMap[$stock_symbol]["value2Percent"] = round( 100*$tableMap[$stock_symbol]["value"]/$tableMap[$stock_symbol]["value2"] );
			$tableMap[$stock_symbol]["valuePercent"] = round( 100*$dt["valuePercent"]);
			$tableMap[$stock_symbol]["valuePercentMax"] = round( 100*$dt["valuePercentMax"]);
			if($dt["ref"]!=0) $tableMap[$stock_symbol]["closePercent"] = round( 100*($dt["close"] - $dt["ref"])/$dt["ref"], 2);
			$tableMap[$stock_symbol]["market"] = $dt["market"];
			$tableMap[$stock_symbol]["buyVolx3"] = 3*10*round( 0.1*0.1*$this->capital/$dt["close"]);
			$tableMap[$stock_symbol]["buyVolx2"] = 2*10*round( 0.1*0.1*$this->capital/$dt["close"]);
			$tableMap[$stock_symbol]["buyVolx1"] = 1*10*round( 0.1*0.1*$this->capital/$dt["close"]);
			$tableMap[$stock_symbol]["symbol"] = $dt["stock_symbol"];
			
			
			for($i=0; $i<20; $i++){
				$tableMap[$stock_symbol]["closeP".$i] = round($dtArray[$i]["closePercent"],2);
				if($total["closeP+".$i]==null) $total["closeP+".$i] = 0;
				if($total["closeP-".$i]==null) $total["closeP-".$i] = 0;
				if($tableMap[$stock_symbol]["closeP".$i]>0) $total["closeP+".$i] ++;
				else $total["closeP-".$i] ++;
			}
		}
		for($i=0; $i<20; $i++){
			$tableMap["TOTAL"]["closeP".$i] = round(100*$total["closeP+".$i] / ($total["closeP+".$i] + $total["closeP-".$i]) , 2);
		}
		/*
		$sortColumnArray = array("value", "value2", "value2Percent", "megaCount", "minMax", "minClose", "maxClose");
		foreach($sortColumnArray as $i=>$value){
			stock_analyser_TableView::$sortColumn = $value;
			uasort($tableMap,"sortFunc");
			$i=0;
			foreach($tableMap as $symbol=>$dt){
				$i++;
				$tableMap[$symbol]["STT__".stock_analyser_TableView::$sortColumn] = $i;
			}
		}
		*/
		//stock_analyser_TableView::$sortColumn = "megaCount";
		//stock_analyser_TableView::$sortColumn = "value2Percent";
		//stock_analyser_TableView::$sortColumn = "ceilPercent";
		
		if($filterType=="value2") {
			stock_analyser_TableView::$sortColumn = "megaCount";
			//stock_analyser_TableView::$sortColumn = "minMax";
		}
		if($filterType=="minClose") {
			//stock_analyser_TableView::$sortColumn = "megaCount";
			//stock_analyser_TableView::$sortColumn = "ceilPercent";
			//stock_analyser_TableView::$sortColumn = "maxClose";
			stock_analyser_TableView::$sortColumn = "minClose";
			//stock_analyser_TableView::$sortColumn = "minMax";
			//stock_analyser_TableView::$sortColumn = "valueSub";
			//stock_analyser_TableView::$sortColumn = "valuePercentMax";
			//stock_analyser_TableView::$sortColumn = "minCloseZ";
			//stock_analyser_TableView::$sortColumn = "minMaxZ";
			//stock_analyser_TableView::$sortColumn = "valuePercent";
			//stock_analyser_TableView::$sortColumn = "value2";
			////stock_analyser_TableView::$sortColumn = "maPer1";
		}
		
		if($filterType=="value2") {
			$i=0;
			foreach($tableMap as $symbol=>$dt){
				if($symbol=="TOTAL") continue;
				
				//if($priceDtArrayMap[$symbol][0]["isDevided"]!=1 && $dt["value2"]>5000000 && $dt["megaCount"]>=2.5) $tableMap[$symbol]["rate"] += -5000;
				//if($dt["value2"]>5000000 && $dt["megaCount"]>=2.5) $tableMap[$symbol]["rate"] += -5000;
			}
		}
		if($filterType=="minClose") {
			$i=0;
			foreach($tableMap as $symbol=>$dt){
				$i++;
				if($symbol=="TOTAL") continue;
				//if($tableMap[$symbol]["minClose"]<=20) $tableMap[$symbol]["rate"] += -5000;
				//if($tableMap[$symbol]["minClose"]==null ) unset($tableMap[$symbol]);
				//if($tableMap[$symbol]["megaCount"] <= 2.5) unset($tableMap[$symbol]);
				//if($priceDtArrayMap[$symbol][0]["isDevided"]!=1 && $dt["value2"]>3000000 && $dt["megaCount"]>=2.5) $tableMap[$symbol]["rate"] += -5000;
				//if($dt["value2"]>3000000 && $dt["megaCount"]>=2.5) $tableMap[$symbol]["rate"] += -5000;
				//if($this->arrayRate[$symbol]==null) $tableMap[$symbol]["rate"] += -5000;
				/*
				if($dt["minClose"]<20 && ($dt["valuePercentMax"]>=300 || $dt["valuePercent"]>=300)){
					$tableMap[$symbol]["rate"] += -5000;
					if($dt["closePercent"]<=-1) $tableMap[$symbol]["rate"] += -2000;
				}
				*/
				/*
				if($this->arrayRate[$symbol]==null){
					if(
						($tableMap[$symbol]["market"]=="ho" && $tableMap[$symbol]["closePercent"]>=2) ||
						($tableMap[$symbol]["market"]=="ha" && $tableMap[$symbol]["closePercent"]>=3)
					) $tableMap[$symbol]["rate"] += -5000;
				}
				*/
			}
			//uasort($tableMap,"sortFuncInverse");
		}
		
		if(stock_analyser_TableView::$sortColumn == "minCloseZZZ" || stock_analyser_TableView::$sortColumn == "maPer1") uasort($tableMap,"sortFuncInverse");
		else uasort($tableMap,"sortFunc");
		$i=0;
		foreach($tableMap as $symbol=>$dt){
			$i++;
			$tableMap[$symbol]["rate"] += $i;
		}
		
		/*
		stock_analyser_TableView::$sortColumn = "minClose";
		uasort($tableMap,"sortFuncInverse");
		$i=0;
		foreach($tableMap as $symbol=>$dt){
			$i++;
			$tableMap[$symbol]["rate"] += $i;
		}
		*/
		
		foreach($this->arrayRate as $symbol=>$rate){
			$tableMap[$symbol]["rate"] += $rate;
		}
		
		stock_analyser_TableView::$sortColumn = "rate";
		uasort($tableMap,"sortFuncInverse");
		
		$i=0;
		foreach($tableMap as $symbol=>$dt){
			$i++;
			/////////if($i>=107) unset($tableMap[$symbol]);
			//if($symbol=="TOTAL") continue;
			//if($tableMap[$symbol]["megaCount"] < 3) unset($tableMap[$symbol]);
		}
		
		if($filterType=="value2") {
			/*
			stock_analyser_TableView::$sortColumn = "minMax";
			uasort($tableMap,"sortFunc");
			
			$i=0;
			foreach($tableMap as $symbol=>$dt){
				$i++;
				$tableMap[$symbol]["rate"] = $i;
			}
			foreach($this->arrayRate as $symbol=>$rate){
				$tableMap[$symbol]["rate"] += $rate;
			}
			*/
			stock_analyser_TableView::$sortColumn = "rate";
			$i=0;
			foreach($tableMap as $symbol=>$dt){
				$i++;
				//$priceDtArrayMap[$symbol][0]["isDevided"]!=1
				//if($dt["value2"]>3000000 && $dt["megaCount"]>=3.5) $tableMap[$symbol]["rate"] += -5000;
				if($dt["value2"]>3000000) $tableMap[$symbol]["rate"] += -5000;
			}
			uasort($tableMap,"sortFuncInverse");
		}
		
		if($filterType=="minClose") {
			stock_analyser_TableView::$sortColumn = "rate";
			$i=0;
			foreach($tableMap as $symbol=>$dt){
				$i++;
				//$priceDtArrayMap[$symbol][0]["isDevided"]!=1
				//if($dt["value2"]>3000000 && $dt["megaCount"]>=3.5) $tableMap[$symbol]["rate"] += -5000;
				//if($dt["maPercent12"]>=100 || ($dt["maPercent1"]>=100 && $dt["maPercent2"]>=100)) $tableMap[$symbol]["rate"] += -5000;
				//if($dt["maPercent1"]>=100 && $dt["maPercent2"]>=100) $tableMap[$symbol]["rate"] += -5000;
				//if($dt["maPercent12"]>=100) $tableMap[$symbol]["rate"] += -5000;
				//if($dt["maPercent12"]>=99) $tableMap[$symbol]["rate"] += -5000;
				//if($dt["maPercent1"]>=100 && $dt["maPercent2"]>=100) $tableMap[$symbol]["rate"] += -1000;
				//if(abs($dt["minClose"])<=20 && $dt["maPercent1"]>=100 && $dt["maPercent2"]>=100) $tableMap[$symbol]["rate"] += -6000;
				//if($dt["maPercent12"]>=100 && $dt["minMax20"]>=10 && $dt["minClose5"]<=7) $tableMap[$symbol]["rate"] += -6000;
				
				///////if($dt["maPercent12"]>=100 && $priceDtArrayMap[$symbol][0]["minMax20"]>=7 && $priceDtArrayMap[$symbol][0]["minMax5"]<=10) $tableMap[$symbol]["rate"] += -6000;
				///////if($priceDtArrayMap[$symbol][0]["greatAll"]>=8) $tableMap[$symbol]["rate"] += -6000;
				if($priceDtArrayMap[$symbol][0]["minMax20"]>=7 && $priceDtArrayMap[$symbol][0]["minClose5"]<=14 && $priceDtArrayMap[$symbol][0]["greatAll"]>=9) $tableMap[$symbol]["rate"] += -6000;
				//if($priceDtArrayMap[$symbol][0]["minMax20"]>=7 && $priceDtArrayMap[$symbol][0]["minClose5"]<=14) $tableMap[$symbol]["rate"] += -6000;
				
				//if(abs($dt["maPer1"])<=2 && $dt["maPercent1"]>=100 && $dt["maPercent2"]>=100) $tableMap[$symbol]["rate"] += -10000;
				//if($dt["maPercent1"]>=100 && $dt["maPercent2"]>=100) $tableMap[$symbol]["rate"] += -2000;
				
				///////////////if($dt["value2"]>=10000000) $tableMap[$symbol]["rate"] += -10000;
				//if($dt["maPercent1"]>=100) $tableMap[$symbol]["rate"] += -2000;
				//if($dt["maPercent2"]>=100) $tableMap[$symbol]["rate"] += -2000;
				$tableMap[$symbol]["rate"] += -4000;
				///////if($tableMap[$symbol]["minClose"]<=20) $tableMap[$symbol]["rate"] += -10000;
				
				
				if($this->arrayRate[$symbol]!=null) continue;
				//if(($dt["maPercent1"]<100 || $dt["maPercent2"]<100) && ($dt["value2"]<3000000 || $dt["megaCount"]<2.5)) unset($tableMap[$symbol]);
			}
			uasort($tableMap,"sortFuncInverse");
			/*
			$i=0;
			foreach($tableMap as $symbol=>$dt){
				$i++;
				$tableMap[$symbol]["rate"] = $i;
				if($tableMap[$symbol]["minClose"] == null) $tableMap[$symbol]["rate"] += 5000;
			}
			foreach($this->arrayRate as $symbol=>$rate){
				$tableMap[$symbol]["rate"] += $rate;
			}
			
			stock_analyser_TableView::$sortColumn = "rate";
			uasort($tableMap,"sortFuncInverse");
			*/
		}
		
		$this->saveJson($this->tableDtArrayMapFile, $tableMap);
		return $tableMap;
		//@>
	}
	public static $sortColumn = "ceilPercent";
}